//
//  DetailController.m
//  吃货·上海
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "DetailController.h"
#import "DetailCell.h"
#import "ShareCoreData.h"

@interface DetailController () <UITableViewDataSource , UITableViewDelegate , UIAlertViewDelegate ,UIAlertViewDelegate>

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic,strong)UIButton *btn;

@end

static NSString *cellIndextifier = @"cell";
@implementation DetailController

- (void)dealloc
{
   DetailCell *cell = self.tableView.visibleCells[0];
    [cell.timer invalidate];
    cell.timer = nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
  
    self.navigationController.navigationBar.translucent =NO;

    //收藏
    self.btn =[UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(KScreenWidth - 30, 0, 30, 30);
    
    //这里进行用户是否登陆状态的判断
    
    if ([[ShareCoreData shareInstance]selectOneDataWithTitle:_model.title]) {
        [self.btn setImage:[UIImage imageNamed:@"iconfont-shoucangyishoucang-copy-copy"] forState:UIControlStateNormal];
        self.btn.userInteractionEnabled = NO;
    }
    else
    {
        [_btn setImage:[UIImage imageNamed:@"iconfont-shoucang"] forState:UIControlStateNormal];
    }
    
    [_btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    self.btn.backgroundColor = KMyColor;
    UIBarButtonItem *btn1 = [[UIBarButtonItem alloc]initWithCustomView:_btn];
    self.navigationItem.rightBarButtonItem = btn1;

    self.tableView.separatorStyle = NO;
    self.tableView.dataSource =self;
    self.tableView.delegate =self;
    
    [self.view addSubview: self.tableView];
    [self.tableView registerClass:[DetailCell class] forCellReuseIdentifier:cellIndextifier];
}

#pragma mark -  收藏
- (void)btn:(UIButton *)btn
{
    if ([[ShareCoreData shareInstance]selectOneDataWithTitle:_model.title]) {
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:@"已收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        btn.userInteractionEnabled = NO;
    }
    else
    {
        [[ShareCoreData shareInstance]insertData:_model];
    }
    
    [btn setImage:[UIImage imageNamed:@"iconfont-shoucangyishoucang-copy-copy"] forState:UIControlStateNormal];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndextifier forIndexPath:indexPath];
    
    cell.model = _model;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.backgroundColor = KMyColor;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [DetailCell heightForCell:_model];
}


@end
